<template>
  <div>
    <!------------------------面包屑--------------------------->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{path:'/blank'}"><i class="el-icon-guide" style="color: black"></i>班级管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{path:'/class'}">班级管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加班级</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <!------------------------添加题库------------------------>
    <el-card style="margin: 0 auto;width: 500px">
      <h1 style="font-size: 20px">添加班级</h1>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" style="margin-top: 20px">
      <el-form-item label="班级名称" prop="className">
        <el-input v-model="ruleForm.className"></el-input>
      </el-form-item>
      <el-form-item label="选择专业" prop="majorId" style="text-align: left">
        <el-select v-model="ruleForm.majorId" placeholder="请选择专业">
          <el-option
              v-for="item in majorList"
              :key="item.id"
              :label="item.majorName"
              :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item style="float: left">
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button type="warning" @click="$router.push('/class')">返回列表</el-button>
      </el-form-item>
    </el-form>
    </el-card>
  </div>
</template>

<script>
let localJwt = localStorage.getItem('localJwt');
export default {
  data() {
    return {
      majorList:[],
      ruleForm: {
        className: '',
        majorId: '',
      },
      rules: {
        majorName: [
          {required: true, message: '请输入名称', trigger: 'blur'},
          {min: 1, max: 25, message: '长度在 2 到 25 个字符', trigger: 'blur'}
        ],
        className: [
          {required: true, message: '请输入名称', trigger: 'blur'},
          {min: 1, max: 25, message: '长度在 1 到 25 个字符', trigger: 'blur'}
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = "http://localhost:9284/class/add-new";
          console.log('url = ' + url);
          let formData = this.qs.stringify(this.ruleForm)
          console.log('formData = ' + formData);

          this.axios
              .create({
                'headers': {
                  'Authorization': localJwt
                }
              })
              .post(url, formData).then((response) => {
            let jsonResult = response.data;
            const h = this.$createElement;

            if (jsonResult.state == 20000) {
              this.$notify({
                title: '添加成功',
                message: h('i', {style: 'color: teal'}, jsonResult.message),
                type: 'success'
              });
              this.resetForm(formName);
              if (jsonResult.jwt!=null){
                localStorage.setItem("localJwt",jsonResult.jwt);
              }
            }else if(jsonResult.state==70100){
              this.$alert(jsonResult.message, '错误', {
                confirmButtonText: '确定',
                callback: action => {
                }
              });
              this.$router.push('/')
            }  else {
              this.$notify.error({
                title: '添加失败',
                message: h('i', {style: 'color: red'}, jsonResult.message),
              });
            }
          })
          console.log('添加完毕！')
        } else {
          console.log('添加失败!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    loadMajorList(){
      //http://localhost:9282/bank/listMajor
      let url = 'http://localhost:9282/bank/listMajor';
      this.axios
          .create({
            'headers': {
              'Authorization': localJwt
            }
          })
          .get(url).then((response) =>{
        let jsonResult = response.data;
        console.log(jsonResult);
        if (jsonResult.state == 20000){
          this.majorList = jsonResult.data;
          if (jsonResult.jwt!=null){
            localStorage.setItem("localJwt",jsonResult.jwt);
          }
        }else if(jsonResult.state==70100){
          this.$alert(jsonResult.message, '错误', {
            confirmButtonText: '确定',
            callback: action => {
            }
          });
          this.$router.push('/')
        } else {
          this.$alert(jsonResult.message,'错误',{
            confirmButtonText:'确认',
            callback: action => {// eslint-disable-line no-unused-vars
              this.$router.back();
            }
          })
        }
      });

    }
  },
  mounted() {
    this.loadMajorList();
  }
}
</script>

<style scoped>

</style>
